//
//  HSFeedBackController.swift
//  HomeSchoolDemo
//
//  Created by rimi on 16/9/29.
//  Copyright © 2016年 赖虹宇. All rights reserved.
//

import UIKit

class HSFeedBackController: UIViewController,UITextViewDelegate {
    @IBOutlet weak var titleCleanBtn: UIButton!
    @IBOutlet weak var textCleanBtn: UIButton!
     //标题文本视图
    @IBOutlet weak var titleTextView: UITextView!
    //内容文本视图
   
    @IBOutlet weak var detailTextView: UITextView!
    //设置两个全局文本
    var lable = UILabel()
    var titleLable = UILabel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置两个文本视图的代理
      detailTextView.delegate = self
    titleTextView.delegate = self
        //设置清除按钮刚开始的时候是隐藏的
        titleCleanBtn.isHidden = true
        textCleanBtn.isHidden = true
        
        
     //内容lable
     lable = UILabel(frame: CGRect(x: 5, y: 8, width: screenW-20, height: 30))
        lable.text = "请输入您的宝贵意见，帮助我们做的更好"
        lable.textColor = UIColor().colorWithHex(0xb2b2b2)
        lable.font = UIFont.systemFont(ofSize: 18)
        detailTextView.addSubview(lable)
     //标题lable
      titleLable = UILabel(frame: CGRect(x: 5, y: 8, width: 200, height: 30))
        titleLable.text = "请输入标题"
        titleLable.textColor = UIColor().colorWithHex(0xb2b2b2)
        titleLable.font = UIFont.systemFont(ofSize: 18)
        titleTextView.addSubview(titleLable)
    }
    //文本视图的代理方法
    func textViewDidChange(_ textView: UITextView) {
        
        
        //将文本清除
        if textView == detailTextView {
            lable.isHidden = true
            //将清除按钮显示
            textCleanBtn.isHidden = false
        }
        if textView == titleTextView {
            titleLable.isHidden = true
            titleCleanBtn.isHidden = false
        }
    }
    
    
    //标题清除按钮
    @IBAction func titleBtnClean(_ sender: UIButton) {
       titleTextView.text = ""
        titleLable.isHidden = false
        titleLable.text = "请输入标题"
        titleCleanBtn.isHidden = true
        
    }
    
    //反馈内容清除按钮
    @IBAction func textCleanBtn(_ sender: AnyObject) {
        detailTextView.text = ""
        lable.isHidden = false
        lable.text = "请输入您的宝贵意见，帮助我们做的更好"
        textCleanBtn.isHidden = true
    }
    
    //返回按钮监听事件
    @IBAction func backBtnClicked(_ sender: UIButton) {
        dismiss(animated: true, completion: nil)
    }
    //意见提交按钮监听事件
    @IBAction func feedSubmitBtnClicked(_ sender: UIButton) {
       let title = titleTextView.text
       let content = detailTextView.text
        if title == "" {
             setUpAlert(str: "标题不能为空")
            titleTextView.text = ""
            titleLable.isHidden = false
            titleLable.text = "请输入标题"
            titleCleanBtn.isHidden = true
        }
        if content == ""{
            setUpAlert(str: "内容不能为空且不能小于5个字符")
            detailTextView.text = ""
            lable.isHidden = false
            lable.text = "请输入您的宝贵意见，帮助我们做的更好"
            textCleanBtn.isHidden = true
        }
         print("你提交了反馈信息")
        //申请接口
        let url = "ZH-schoolname-S-submitJXOpinion"
        //参数
        let key  = userInfo["key"] as! String
        let param = ["key":key,"title":title!,"content":content!]
        HSNetwordTools.shared.downloadData(urlStr: url, parameters: param) {[weak self] (data, error) in
            guard let newData = data as? NSDictionary else{
                return
            }
            print(newData)
            switch newData["result"] as! Int {
            case 0:
                self?.setUpAlert(str: "意见回馈提交成功")
                //恢复文本输入视图
                self?.titleTextView.text = ""
                self?.titleLable.isHidden = false
                self?.titleLable.text = "请输入标题"
                self?.titleCleanBtn.isHidden = true
           
                self?.detailTextView.text = ""
                self?.lable.isHidden = false
                self?.lable.text = "请输入您的宝贵意见，帮助我们做的更好"
                self?.textCleanBtn.isHidden = true

            case 1:
                self?.setUpAlert(str: "提交失败，请检查网络连接")
            default:
                break
            }
        }
        
        
    }
    
    func setUpAlert(str:String){
        let alertVc = UIAlertController(title: "提示", message: str, preferredStyle: .alert)
        alertVc.addAction(UIAlertAction(title: "确定", style: .default, handler: nil))
        present(alertVc, animated: true, completion: nil)
        
        
        
    }

}
